package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Consumer;

/**
 * Created by 沐风 on 2024/4/12 17:13
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWithDishs(SetmealDTO setmealDTO) {
        //往套餐表添加1份套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);

        //往套餐菜品表添加n份菜品
        //a.先获得添加完套餐之后的套餐id，mabatis的sql语句需要主键返回
        Long id = setmeal.getId();
        //b.往套餐菜品类里面添加套餐id属性的值
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(sd -> sd.setSetmealId(id));
        }

        //c.调用方法插入数据
        setmealDishMapper.addBatch(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult query(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.query(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        //在售状态下，禁止删除套餐
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.queryById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //开始批量删除套餐
        setmealMapper.deleteByIds(ids);
        //套餐菜品表中的对应数据也删除
        setmealDishMapper.deleteByIds(ids);
    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //想要起售套餐时，首先看该套餐内是否有停售的菜品，如果有禁止起售改套餐
        if (status == StatusConstant.ENABLE){
            List<Dish> dishes = dishMapper.queryBySetmealId(id);

           //遍历菜品，调用get方法看该菜品状态字段是停售的话，禁止上架
            if (dishes != null && !dishes.isEmpty()){
                for (Dish dish : dishes) {
                    if (dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }

        }

        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        //先从套餐表查询套餐
        Setmeal setmeal = setmealMapper.queryById(id);
        //还要从套餐菜品表中查询出对应数据
        List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(id);

        //封装数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        //更新套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //更新套餐菜品表中的相关数据
        //a.先根据要删除的套餐的id删除套餐菜品表中保存的相关数据
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);

        //b.在重新插入更改后的相关数据
        //b.1 往套餐菜品类里面添加套餐id属性的值
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(sd -> sd.setSetmealId(setmealId));
        }

        //b.2 查看添加的菜品的状态，如果为停售，那么禁止上架该套餐
        for (SetmealDish setmealDish : setmealDishes) {
            //b.2.1 先根据菜品id在菜品表中查询菜品
            Dish dish = dishMapper.queryById(setmealDish.getDishId());

            //b.2.2 在调用get方法看该菜品状态字段是停售的话，禁止上架
            if (dish.getStatus() == StatusConstant.DISABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        //c.2 调用方法插入数据
        setmealDishMapper.addBatch(setmealDishes);

    }

    /**
     * 根据分类id查询套餐 ，条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
